import math

def weighted_cosine_similarity(features1, features2):
    """带权重的余弦相似度计算"""
    all_keys = set(features1.keys()).union(set(features2.keys()))
    weights = {
        'Join': 2.0,
        'Subquery': 1.5,
        'Column': 0.8,
        'depth0': 1.2,
        'depth1': 1.1,
        'depth2': 1.0
    }
    
    dot_product = 0
    magnitude1 = 0
    magnitude2 = 0
    
    for key in all_keys:
        # 计算权重因子
        weight = 1.0
        for k, w in weights.items():
            if k in key:
                weight = w
                break
        
        v1 = features1.get(key, 0) * weight
        v2 = features2.get(key, 0) * weight
        
        dot_product += v1 * v2
        magnitude1 += v1 ** 2
        magnitude2 += v2 ** 2
    
    magnitude1 = math.sqrt(magnitude1)
    magnitude2 = math.sqrt(magnitude2)
    
    if magnitude1 == 0 or magnitude2 == 0:
        return 0.0
    
    similarity = dot_product / (magnitude1 * magnitude2)
    
    # 应用非线性变换增强区分度
    return similarity ** 1.5